//
//  CLHTTPRequest.m
//  CarLife
//
//  Created by yu on 2017/7/31.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "HTTPRequest.h"
#import "InterfacedConst.h"
#import "PPNetworkHelper.h"
#import "ErrorInfoUtility.h"

@implementation HTTPRequest

+ (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field {
    [PPNetworkHelper setValue:value forHTTPHeaderField:field];
}

//+ (void)setResponseSerializer:(PPResponseSerializer)responseSerializer {
//    [PPNetworkHelper setResponseSerializer:PPResponseSerializerJSON];
//}

/*
 配置好PPNetworkHelper各项请求参数,封装成一个公共方法,给以上方法调用,
 相比在项目中单个分散的使用PPNetworkHelper/其他网络框架请求,可大大降低耦合度,方便维护
 在项目的后期, 可以在公共请求方法内任意更换其他的网络请求工具,切换成本小
 */

#pragma mark - 请求的公共方法

// TODO:POST请求
+ (NSURLSessionTask *)postRequestWithURL:(NSString *)URL parameters:(id)parameter show:(BOOL)show success:(RequestSuccess)success failure:(RequestFailure)failure
{
    // 在请求之前你可以统一配置你请求的相关参数 ,设置请求头, 请求参数的格式, 返回数据的格式.
    if ([PPNetworkHelper isNetwork]) {
        [PPNetworkHelper closeLog];
        if (show) [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
        return [PPNetworkHelper POST:URL parameters:parameter success:^(id responseObject) {
            // 在这里可以根据项目自定义其他一些重复操作,比如加载页面时候的等待效果, 提醒弹窗.
            if (show) [MBProgressHUD hideAllHUDsForView:KEY_WINDOW animated:YES];
            success(responseObject);
        } failure:^(NSError *error) {
            if (show) [MBProgressHUD hideAllHUDsForView:KEY_WINDOW animated:YES];
            failure(error);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD showToast:[ErrorInfoUtility af_errorWithCode:error.code]];
            });
        }];
    } else {
        [MBProgressHUD showToast:@"网络已断开,请检查网络设置"];
        return nil;
    }
}

// TODO:GET请求
+ (NSURLSessionTask *)getRequestWithURL:(NSString *)URL parameters:(id)parameter show:(BOOL)show success:(RequestSuccess)success failure:(RequestFailure)failure
{
    if ([PPNetworkHelper isNetwork]) {
        [PPNetworkHelper closeLog];
        if (show) [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
        return [PPNetworkHelper GET:URL parameters:parameter success:^(id responseObject) {
            if (show) [MBProgressHUD hideAllHUDsForView:KEY_WINDOW animated:YES];
            success(responseObject);
        } failure:^(NSError *error) {
            if (show) [MBProgressHUD hideAllHUDsForView:KEY_WINDOW animated:YES];
            failure(error);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD showToast:[ErrorInfoUtility af_errorWithCode:error.code]];
            });
        }];
    } else {
        [MBProgressHUD showToast:@"网络已断开,请检查网络设置"];
        return nil;
    }
}

// TODO:POST请求(多张图片同时上传)
+ (NSURLSessionTask *)uploadImagesWithURL:(NSString *)URL parameters:(id)parameters name:(NSString *)name images:(NSArray<UIImage *> *)images imageScale:(CGFloat)imageScale imageType:(NSString *)imageType progress:(HttpProgress)progresses success:(RequestSuccess)success failure:(RequestFailure)failure
{
    if ([PPNetworkHelper isNetwork]) {
        [PPNetworkHelper closeLog];
        [MBProgressHUD showLoading:@"正在处理"];
        return [PPNetworkHelper uploadImagesWithURL:URL parameters:parameters name:name images:images fileNames:nil imageScale:imageScale imageType:imageType progress:^(NSProgress *progress) {
            CGFloat value = progress.fractionCompleted*100;
            //[MBProgressHUD showLoading:NSStringFormat(@"%f",value)];
            progresses(value);
        } success:^(id responseObject) {
            success (responseObject);
            [MBProgressHUD hideAllHUDsForView:KEY_WINDOW animated:YES];
        } failure:^(NSError *error) {
            [MBProgressHUD hideAllHUDsForView:KEY_WINDOW animated:YES];
            failure(error);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD showToast:[ErrorInfoUtility af_errorWithCode:error.code]];
            });
        }];
    } else {
        [MBProgressHUD showToast:@"网络已断开,请检查网络设置"];
        return nil;
    }
}

// TODO:POST请求(单张张图片上传666)
+ (void)uploadImageWithURL666:(NSString *)URL eqId:(NSString *)eqId types:(NSArray<NSString *> *)types parameters:(id)parameters name:(NSString *)name images:(NSArray<UIImage *> *)images imageScale:(CGFloat)imageScale imageType:(NSString *)imageType success:(void (^)(int status))success failure:(RequestFailure)failure
{
    if ([PPNetworkHelper isNetwork]) {
        [MBProgressHUD showLoading:@"正在处理..."];
        __block NSInteger uploadImgCount = 0;
        for (NSInteger i = 0; i < images.count; i++) {
            NSDictionary *params = @{@"eqId":eqId,@"type":types[i]};
            [PPNetworkHelper uploadImagesWithURL:URL parameters:params name:name images:@[images[i]] fileNames:nil imageScale:imageScale imageType:imageType progress:nil success:^(id responseObject) {
                PPLog(@"单张图片上传666 = %@",responseObject);
                if ([responseObject[@"status"] isEqual:@(0)]) {
                    uploadImgCount ++;
                    if (images.count == uploadImgCount) {
                        success (0);
                        [MBProgressHUD hideAllHUDsForView:KEY_WINDOW animated:YES];
                    }
                }
            } failure:^(NSError *error) {
                [MBProgressHUD hideAllHUDsForView:KEY_WINDOW animated:YES];
                failure(error);
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [MBProgressHUD showToast:[ErrorInfoUtility af_errorWithCode:error.code]];
                });
            }];
        }
    } else {
        [MBProgressHUD showToast:@"网络 已断开,请检查网络设置"];
    }
}

// TODO:POST请求(单张张图片上传)
+ (NSURLSessionTask *)uploadImageWithURL:(NSString *)URL parameters:(id)parameters name:(NSString *)name images:(NSArray<UIImage *> *)images imageScale:(CGFloat)imageScale imageType:(NSString *)imageType progress:(HttpProgress)progresses success:(RequestSuccess)success failure:(RequestFailure)failure
{
        if ([PPNetworkHelper isNetwork]) {
            [PPNetworkHelper closeLog];
            return [PPNetworkHelper uploadImagesWithURL:URL parameters:parameters name:name images:images fileNames:nil imageScale:imageScale imageType:imageType progress:^(NSProgress *progress) {
                CGFloat value = progress.fractionCompleted*100;
                progresses(value);
            } success:^(id responseObject) {
                success (responseObject);
            } failure:^(NSError *error) {
                failure(error);
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [MBProgressHUD showToast:[ErrorInfoUtility af_errorWithCode:error.code]];
                });
            }];
        } else {
            [MBProgressHUD showToast:@"网络已断开,请检查网络设置"];
            return nil;
        }
}


@end
